#include <iostream>

using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode head = ListNode(0);
        ListNode* node = &head;
        while (l1 != nullptr && l2 != nullptr) {
            if (l1->val < l2->val) {
                node->next = l1;
                l1 = l1->next;
            } else {
                node->next = l2;
                l2 = l2->next;
            }
            node = node->next;
        }
        while (l1 != nullptr) {
            node->next = l1;
            l1 = l1->next;
            node = node->next;
        }
        while (l2 != nullptr) {
            node->next = l2;
            l2 = l2->next;
            node = node->next;
        }
        return head.next;
    }
};
